package com.company;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.logging.Logger;

/**
 *
 */
public class TaobaoCrawler {
    private String productName;
    public TaobaoCrawler(String productName){
        this.productName = productName;
    }

    private String getRequestUrl(int indexStart){
        return "https://s.taobao.com/search?q="+productName
                +"&p4ppushleft=5%2C1&s="+indexStart
                +"&cps=yes&app=vproduct&cd=false&v=auction&tab=all&vlist=1";
    }

    public int getProductNumber(){
        String url = getRequestUrl(0);
        Document document;
        String totalStringKey = "totalCount";
        int retry = 5;
        while (retry > 0){
            document = JsoupUtils.getRequest(url);
            String webText = document.data();
            int index = webText.indexOf(totalStringKey);
            if ( index >= 0 ){
                String tmp = webText.substring(index);
                String totalString = tmp.substring(totalStringKey.length()+2, tmp.indexOf("}}"));
                return Integer.parseInt(totalString);
            }
            retry --;
        }
        return -1;
    }

    public JSONArray getItems(int indexStart){
        String url = getRequestUrl(indexStart);
        Elements scriptElements = null;
        while(true){
            Document document = JsoupUtils.getRequest(url);
            scriptElements = document.getElementsByTag("script");
            if ( scriptElements == null || scriptElements.size() < 9 ){
                try {
                    Thread.sleep(new Random().nextInt(3000));
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }else{
                break;
            }
        }

        if (scriptElements != null) {
            scriptElements.size();
        }

        String jsonString = null;
        for ( Element element : scriptElements ) {
            if( element.data().contains("g_page_config") ){
                jsonString = element.data()
                        .replaceAll(" " , "")
                        .replaceAll("\r", "")
                        .replaceAll("\n", "")
                        .replaceFirst("g_page_config=", "");
                jsonString = jsonString.substring(0, jsonString.indexOf(";g_srp_loadCss();"));
                //System.out.println(jsonString);
                break;
            }
        }

        if ( jsonString != null ){
            JSONObject jsonObject = (JSONObject) JSONObject.parse(jsonString);
            jsonObject = (JSONObject) jsonObject.get("mods");
            jsonObject = (JSONObject) jsonObject.get("itemlist");
            jsonObject = (JSONObject) jsonObject.get("data");
            JSONArray jsonArray = jsonObject.getJSONArray("auctions");
            return jsonArray;
        }
        return null;
    }

}
